const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    year: ['请选择年份'],
    shiqu: ['请选择市区'],
    xianqu: ['请选择县区'],
    xueli: ['请选择学历'],
    name: '',
    zhuanye: '',
    phone: 0,
    theList: '',
    flag:1
  },

  zhuanyeInput: function (e) {
    this.setData({
      zhuanye: e.detail.value
    })
    console.log(this.data.zhuanye)
  },

  bindPickerChange: function (e) {
    let num = e.detail.value;
    var that = this;
    console.log('shi发送选择改变，携带值为', that.data.year[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42274&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'nianfen',
        grtext: that.data.year[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择市区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].shi_qu)
        }
        that.setData({
          shiqu: list
        });
        console.log(that.data.shiqu)
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('xianqu发送选择改变，携带值为', that.data.shiqu[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42274&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'shi_qu',
        grtext: that.data.shiqu[num2],
        onefiled: 'nianfen',
        onetext: that.data.year[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择县区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].quxian)

        }
        that.setData({
          xianqu: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let that = this;
    console.log('suoshudanwei发送选择改变，携带值为', that.data.xianqu[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42274&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'quxian',
        grtext: that.data.xianqu[num3],
        onefiled: 'nianfen',
        onetext: that.data.year[this.data.index],
        twofiled: 'shi_qu',
        twotext: that.data.shiqu[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择学历')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].xueli)
        }
        that.setData({
          xueli: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let that = this;
    console.log('gangweibianma发送选择改变，携带值为', that.data.xueli[num4]);
    this.setData({
      index4: e.detail.value
    })
  },


  onLoad: function (options) {
    var that = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=42274&callback=?', //接收url
      data: {
        level: '1',
        nianfen: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        console.log(theJson)
        list.push('请选择年份')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].nianfen)
        }
        that.setData({
          year: list
        });
        console.log(that.data.year)
      }
    })
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },

  b1: function () {
    wx.navigateTo({
      url: '../list/list',
    })
  },
  tijiao() {
      var that = this;
      var year = that.data.year[that.data.index];
      var shiqu = that.data.shiqu[that.data.index2];
      var xianqu = that.data.xianqu[that.data.index3];
      var xueli = that.data.xueli[that.data.index4];
      var zhuanye = that.data.zhuanye; 
        var zhuanyeSt = {
          year: year,
          shiqu: shiqu,
          xianqu: xianqu,
          xueli: xueli,
          zhuanye: zhuanye,
        }
        wx.setStorageSync('zhuanyeSt', zhuanyeSt)
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/chaxun/getlist?actid=42274&callback=?', //接收url
          data: {
            nianfen: year,
            shi_qu: shiqu,
            quxian: xianqu,
            xueli: xueli,
            zhuanye: zhuanye,
            fenxiao: that.data.area,
            geneal: that.data.geneal,
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            
            that.setData({
              theList: theJson.lists,
              flag:0
            })
            if(that.data.theList==undefined){
              wx.showToast({
                title: '此城市对应学历层次下无此专业分数',
                duration: 2000,
                icon: 'none'
              })
            }
            console.log(that.data.theList)
            console.log(theJson)
          }
        })
      
  
      
    },
    lingqu(){
      wx.navigateTo({
        url: '../web1/web1' ,
      })
    },
  getPhoneNumber(e) {
    var that = this;
    var year = that.data.year[that.data.index];
    var shiqu = that.data.shiqu[that.data.index2];
    var xianqu = that.data.xianqu[that.data.index3];
    var xueli = that.data.xueli[that.data.index4];
    var zhuanye = that.data.zhuanye;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
       wx.setStorageSync('phone',res.result)
      console.log(res.result)
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 37,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      
      var zhuanyeSt = {
        year: year,
        shiqu: shiqu,
        xianqu: xianqu,
        xueli: xueli,
        zhuanye: zhuanye,
        phone: res.result
      }
      wx.setStorageSync('zhuanyeSt', zhuanyeSt)
     
     
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/getlist?actid=42274&callback=?', //接收url
        data: {
          nianfen: year,
          shi_qu: shiqu,
          quxian: xianqu,
          xueli: xueli,
          zhuanye: zhuanye,
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          
          that.setData({
            theList: theJson.lists,
            flag:0
          })
          if(that.data.theList==undefined){
            wx.showToast({
              title: '此城市对应学历层次下无此专业分数',
              duration: 2000,
              icon: 'none'
            })
          }
          console.log(that.data.theList)
          console.log(theJson)



        }
      })
    }).catch(err => {
      console.error(err);
    });

    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})